package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"mysqlbackup/cron"
	"strings"
)

var path string

var c = cron.New()
var config Config

var install = &cobra.Command {
	Use: "install",
	Short: "安装执行周期",
	Args: func(cmd *cobra.Command, args []string) error {
		path = strings.Replace(path, " ", "", -1)
		path = strings.Replace(path, "\n", "", -1)
		if len(path) <= 0 {
			return fmt.Errorf("path 配置文件路径是必须的")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		var err error
		config,err = getConfig(path)
		if err != nil {
			Error(cmd, args, err)
		}
		err =c.AddAll(config.Crons)
		if err != nil {
			Error(cmd, args, err)
		}
		select {}
	},
}



func init(){
	install.Flags().StringVarP(&path,"path","p","","path 配置文件路径")
	install.MarkFlagDirname("path")
	rootCmd.AddCommand(install)
}